package com.make.sunflower;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MybatisPlusCodeGenerator {
    public static final String url = "jdbc:mysql://192.168.3.11:3306/sunflower";
    public static final String username = "root";
    public static final String password = "123456";
    /*数据源配置*/
    public static final DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(url,username,password);

    public static void main(String[] args) {

        /*全局配置*/
        GlobalConfig globalConfig = new GlobalConfig.Builder()
//                .fileOverride()
                .outputDir(System.getProperty("user.dir")+"/src/main/java")
                .author("chenjiaxin")
                .commentDate("yyyy-MM-dd")
                .disableOpenDir()
                .build();
        /*包配置*/
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.make.sunflower")
                .entity("entity")
                .controller("controller")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("dao")
                .xml("dao.xml")
                .build();
        /*策略配置*/
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .entityBuilder()
                .enableLombok()
                .enableChainModel()
                .logicDeleteColumnName("deleted")
                .build()
                /*controller配置*/
                .controllerBuilder()
                .enableHyphenStyle()
                .enableRestStyle()
                .formatFileName("%sController")
                .build()
                /*service配置*/
                .serviceBuilder()
                .formatServiceFileName("%sIService")
                .formatServiceImplFileName("%sService")
                .build()
                /**/
                .mapperBuilder()
                .enableBaseResultMap()
                .enableMapperAnnotation()
                .formatMapperFileName("%sMapper")
                .formatXmlFileName("%sMapper")
                .build();
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfigBuilder.build());
        autoGenerator.global(globalConfig);
        autoGenerator.packageInfo(packageConfig);
        autoGenerator.strategy(strategyConfig);
        autoGenerator.strategy(strategyConfig);
        autoGenerator.execute();

    }

    public void test(){
        FastAutoGenerator.create(dataSourceConfigBuilder)
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？")).fileOverride())
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder().enableLombok().addTableFills(
                                new Column("create_time", FieldFill.INSERT)
                        ).build())
                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */
                .execute();
    }
    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
